//
//  DataBaseForPhoto.m
//  世界
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "DataBaseForPhoto.h"
#import <sqlite3.h>
#import "ModelForForPhotoDetail.h"
@interface DataBaseForPhoto ()

@property(nonatomic,retain)ModelForForPhotoDetail *collect;


@end

@implementation DataBaseForPhoto

- (void)dealloc
{
    [_collect release];
    [super dealloc];
}

//单例
+(instancetype)shareDataBaseHandle
{
    //在静态区,只初始化一次
    //因为是在静态区,是全局的,所以第二次执行这个方法的时候,会找有没有dataBase,就是DataBaseHandle *指针指向dataBase的是hi偶,指的是不是空地址,因为第一次执行这个方法的时候,因为一开始是空的,所以创建了一个dataBase,对象,
    static DataBaseForPhoto *dataBase = nil;
    
    if (dataBase == nil)
    {
        dataBase = [[DataBaseForPhoto alloc]init];
        
    }
    return dataBase;
}


//定义一个sqlite结构体(对象)
static sqlite3 *db;
//打开数据库
-(void)open
{
    //API:sqlite3_open
    if (db!=nil)
    {
       // NSLog(@"数据库已开启");
        return;
    }
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Photo.sqlite"];
    
    /**
     *  参数1:数据库文件的路径(类型为C字符串)
     
     参数2:数据库对象
     */
    int result = sqlite3_open(file.UTF8String, &db);
    
    if (result == SQLITE_OK)
    {
//        NSLog(@"打开数据库成功");
    }else
    {
         NSLog(@"打开数据库失败");
        NSLog(@"%d",result);
    }
    
}

//创建表格
-(void)createTable
{
    //API:sqlite3_exec
    //创建一个SQL语句
    NSString *createTable = @"create table if not exists Photo(number integer primary key autoincrement,url text,content text,title text,author text)";
    
    int result = sqlite3_exec(db, createTable.UTF8String, nil,nil,nil);
    
    if (result == SQLITE_OK)
    {
//        NSLog(@"创建表格成功");
    }else
    {
         NSLog(@"创建表格失败");
        NSLog(@"%d",result);
    }
}

//插入数据
-(void)insertCollect:(ModelForForPhotoDetail *)collect
{
    //API:sqlite3_exec
    NSString *insert = [NSString stringWithFormat:@"insert into Photo (url,content,title,author) values ('%@','%@','%@','%@')", collect.url,collect.content,collect.title,collect.author];
    
    int result = sqlite3_exec(db, insert.UTF8String, nil,nil,nil);
    
    if (result == SQLITE_OK)
    {
//        NSLog(@"插入数据成功");
    }else
    {
        NSLog(@"插入数据失败");
        NSLog(@"%d",result);
    }
}

//删除数据
-(void)deleteWithCode:(NSString *)title
{
    //API:sqlite3_exec
    NSString *delete = [NSString stringWithFormat:@"delete from Photo where title = '%@'",title];
    int result = sqlite3_exec(db, delete.UTF8String, nil,nil,nil);
    
    if (result == SQLITE_OK)
    {
//        NSLog(@"清除数据成功");
    }else
    {
         NSLog(@"清除数据失败");
        NSLog(@"%d",result);
    }
}



#pragma mark 查询单个数据
-(BOOL)select:(ModelForForPhotoDetail *)collect
{
    NSMutableArray *array = [NSMutableArray array];
    
    array = [self select];
    
    BOOL isSelect = NO;
    for (ModelForForPhotoDetail *collectOne in array)
    {
        if ([collectOne.title isEqualToString:collect.title])
        {
            isSelect = YES;
            break;
        }else
        {
            isSelect = NO;
        }
    }
    return isSelect;
}





//查询全部数据
-(NSMutableArray *)select
{
    //存放符合条件的数据(元素是Model对象)
    NSMutableArray *array = [NSMutableArray array];
    //sql语句
    NSString *select = @"select * from Photo";
    
    //API:stmt结构体,sqlite3_prepare_v2,sqlite3_step,sqlite3_column
    
    //声明一个stmt对象(结构体)
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, select.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
       // NSLog(@"准备就绪,查询中...");
        
        //每行都执行查询语句
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            
            //如果能进来到while里面,说明有这条记录,不用再写下面的任何步骤,直接写一个返回值,直接return yes,跳出去,外界用返回值判断有没有这个记录,就是有没有这个要找的东西
            
            /**********************  下面的看需不需要写再写  **********************/
            
            //如果查询条件匹配,通过sqlite3_column函数簇取出值
            //取出哪一列的数据值
            const unsigned char *url = sqlite3_column_text(stmt, 1);
            
            const unsigned char *content= sqlite3_column_text(stmt, 2);
            
            const unsigned char *title= sqlite3_column_text(stmt, 3);
            const unsigned char *author = sqlite3_column_text(stmt, 4);
            
            //创建model对象,赋值后放到数组中
            
            ModelForForPhotoDetail *model2 = [[ModelForForPhotoDetail alloc]init];
            
            model2.url = [NSString stringWithUTF8String:(const char *)url];
            
            model2.content= [NSString stringWithUTF8String:(const char *)content];
            
            model2.title = [NSString stringWithUTF8String:(const char *)title];
            
            model2.author = [NSString stringWithUTF8String:(const char *)author];
            
            [array addObject:model2];
        }
        
        //销毁对象
        sqlite3_finalize(stmt);
    }else
    {
        NSLog(@"不能正常查询 %d",result);
    }
    
    return array;
}

//删除表
-(void)dropTable
{
    NSString *drop = @"drop table lanou1130";
    
    int  result = sqlite3_exec(db, drop.UTF8String, nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"除表成功");
    }else{
        NSLog(@"%d",result);
    }
}

//关闭数据库
-(void)closeDB
{
    int result = sqlite3_close(db);
    if (result == SQLITE_OK)
    {
        NSLog(@"guan数据库成功");
    }else
    {
        NSLog(@"%d",result);
    }
}


@end
